﻿#include "RespawnInfo.h"
#include "../../Shared/Functions/Functions.h"

using namespace Server::MirEnvir;

namespace Server::MirDatabase
{

    Server::MirEnvir::Envir *RespawnInfo::getEnvir()
    {
        return Envir::getMain();
    }

    RespawnInfo::RespawnInfo()
    {

    }

    RespawnInfo::RespawnInfo(BinaryReader *reader, int Version, int Customversion)
    {
        MonsterIndex = reader->ReadInt32();

        Location = new Point(reader->ReadInt32(), reader->ReadInt32());

        Count = reader->ReadUInt16();
        Spread = reader->ReadUInt16();

        Delay = reader->ReadUInt16();
        Direction = reader->ReadByte();

        RoutePath = reader->ReadString();

        if (Version > 67)
        {
            RandomDelay = reader->ReadUInt16();
            RespawnIndex = reader->ReadInt32();
            SaveRespawnTime = reader->ReadBoolean();
            RespawnTicks = reader->ReadUInt16();
        }
        else
        {
            setEnvir(getEnvir() + 1);
            RespawnIndex = getEnvir()->RespawnIndex;
        }
    }

    RespawnInfo *RespawnInfo::FromText(const std::string &text)
    {
        std::vector<std::string> data = text.Split({','}, StringSplitOptions::RemoveEmptyEntries);

        if (data.size() < 7)
        {
            return nullptr;
        }

        RespawnInfo *info = new RespawnInfo();

        int x, y;

        if (!int::TryParse(data[0], info->MonsterIndex))
        {
            delete info;
            return nullptr;
        }
        if (!int::TryParse(data[1], x))
        {
            delete info;
            return nullptr;
        }
        if (!int::TryParse(data[2], y))
        {
            delete info;
            return nullptr;
        }

        info->Location = new Point(x, y);

        if (!unsigned short::TryParse(data[3], info->Count))
        {
            delete info;
            return nullptr;
        }
        if (!unsigned short::TryParse(data[4], info->Spread))
        {
            delete info;
            return nullptr;
        }
        if (!unsigned short::TryParse(data[5], info->Delay))
        {
            delete info;
            return nullptr;
        }
        if (!unsigned char::TryParse(data[6], info->Direction))
        {
            delete info;
            return nullptr;
        }
        if (!unsigned short::TryParse(data[7], info->RandomDelay))
        {
            delete info;
            return nullptr;
        }
        if (!int::TryParse(data[8], info->RespawnIndex))
        {
            delete info;
            return nullptr;
        }
        if (!bool::TryParse(data[9], info->SaveRespawnTime))
        {
            delete info;
            return nullptr;
        }
        if (!unsigned short::TryParse(data[10], info->RespawnTicks))
        {
            delete info;
            return nullptr;
        }

//C# TO C++ CONVERTER TODO TASK: A 'delete info' statement was not added since info was used in a 'return' or 'throw' statement.
        return info;
    }

    void RespawnInfo::Save(BinaryWriter *writer)
    {
        writer->Write(MonsterIndex);

        writer->Write(Location->X);
        writer->Write(Location->Y);
        writer->Write(Count);
        writer->Write(Spread);

        writer->Write(Delay);
        writer->Write(Direction);

        writer->Write(RoutePath);

        writer->Write(RandomDelay);
        writer->Write(RespawnIndex);
        writer->Write(SaveRespawnTime);
        writer->Write(RespawnTicks);
    }

    std::string RespawnInfo::ToString()
    {
        return std::string::Format("Monster: {0} - {1} - {2} - {3} - {4} - {5} - {6} - {7} - {8} - {9}", MonsterIndex, Functions::PointToString(Location), Count, Spread, Delay, Direction, RandomDelay, RespawnIndex, SaveRespawnTime, RespawnTicks);
    }

    RouteInfo *RouteInfo::FromText(const std::string &text)
    {
        std::vector<std::string> data = text.Split({','}, StringSplitOptions::RemoveEmptyEntries);

        if (data.size() < 2)
        {
            return nullptr;
        }

        RouteInfo *info = new RouteInfo();

        int x, y;

        if (!int::TryParse(data[0], x))
        {
            delete info;
            return nullptr;
        }
        if (!int::TryParse(data[1], y))
        {
            delete info;
            return nullptr;
        }

        info->Location = new Point(x, y);

        if (data.size() <= 2)
        {
//C# TO C++ CONVERTER TODO TASK: A 'delete info' statement was not added since info was used in a 'return' or 'throw' statement.
            return info;
        }

//C# TO C++ CONVERTER TODO TASK: A 'delete info' statement was not added since info was used in a 'return' or 'throw' statement.
        return !int::TryParse(data[2], info->Delay) ? info : info;
    }
}
